# Most of the Mercurial code does not use fbcode-relative includes, so autodeps cannot
# process it correctly.  Disable autodeps on this file for now.  Eventually we should
# fix the mercurial code to use fbcode-relative includes.
#

load("@fbcode//eden/scm:targets.bzl", "gen_hgpython", "hg_binary")
load("@fbcode_macros//build_defs:cpp_binary.bzl", "cpp_binary")
load("@fbcode_macros//build_defs:cpp_library.bzl", "cpp_library")
load("@fbcode_macros//build_defs:export_files.bzl", "export_file")
load("@fbcode_macros//build_defs:link_group_registry.bzl", "EVICT_FBCODE_DEFAULT_DEV_GROUP_MAP", "PYTHON_EXTREME_LINK_GROUP_THRESHOLD")
load("@fbcode_macros//build_defs:native_rules.bzl", "buck_command_alias", "buck_filegroup", "buck_genrule", "buck_sh_binary")
load("@fbcode_macros//build_defs:python_binary.bzl", "python_binary")
load("@fbcode_macros//build_defs:python_library.bzl", "python_library")
load("@fbcode_macros//build_defs:rust_universal_binary.bzl", "rust_universal_binary")
load("@fbcode_macros//build_defs/lib:rust_oss.bzl", "rust_oss")

oncall("sapling")

cpp_library(
    # @autodeps-skip
    name = "core_headers",
    headers = [
        "sapling/bitmanipulation.h",
        "sapling/compat.h",
    ],
)

cpp_library(
    # @autodeps-skip
    name = "traceprof",
    srcs = ["sapling/ext/extlib/traceprofimpl.cpp"],
    cpp_compiler_flags = [
        "-fno-strict-aliasing",
    ],
    deps = [
        "fbsource//third-party/python:python",
    ],
)

cpp_library(
    name = "libbdiff",
    srcs = [
        "sapling/bdiff.c",
    ],
    exported_deps = [":core_headers"],
)

cpp_library(
    name = "bser",
    srcs = [
        "sapling/ext/extlib/pywatchman/bser.c",
    ],
    compiler_flags = [
        "-fno-strict-aliasing",
        "-Wno-missing-field-initializers",
    ],
    deps = [
        "fbsource//third-party/python:python",
    ],
)

cpp_library(
    name = "libmpatch",
    srcs = [
        "sapling/mpatch.c",
    ],
    compiler_flags = [
        "-Wno-deprecated-declarations",
    ],
    exported_deps = [":core_headers"],
)

python_binary(
    name = "gen_version_py_bin",
    srcs = ["gen_version.py"],
    main_module = "eden.scm.gen_version",
)

# Pure Python first party modules
buck_filegroup(
    name = "pure-modules",
    srcs = glob([
        "sapling/**/*.py",
        "ghstack/**/*.py",
    ]),
)

python_library(
    name = "clone",
    srcs = [
        "fb/staticfiles/opt/facebook/bin/clone.py",
    ],
    resources = glob(["fb/staticfiles/opt/facebook/bin/**"]),
)

python_library(
    name = "redact",
    srcs = [
        # This file is used as a standalone Python library outside Sapling.
        # Do not import Sapling-only modules like `bindings`.
        "sapling/redact.py",
    ],
)

# Similar to "hg".
# The main executable uses argv[0] to decide to run as a Python interpreter.
gen_hgpython(hg_target = "fbcode//eden/scm:hg")

gen_hgpython(
    hg_target = "fbcode//eden/scm:hg_test",
    suffix = "_test",
)

export_file(
    name = "windows-manifest",
    src = "exec/hgmain/manifest.xml",
)

hg_binary(
    name = "hg",
    autocargo = {
        "cargo_target_config": {"name": "hgmain"},
        "cargo_toml_config": {
            "dependencies_override": {
                "dependencies": {
                    "commands": {"default-features": False},
                    "pyblackbox": {"path": "../../saplingnative/bindings/modules/pyblackbox"},
                },
                "target": {
                    '\'cfg(target_os = "linux")\'': {"dependencies": {
                        "dirs": {"optional": True},
                        "sapling-identity": {"optional": True},
                    }},
                    '\'cfg(target_os = "macos")\'': {"dependencies": {
                        "dirs": {"optional": True},
                        "sapling-identity": {"optional": True},
                    }},
                },
            },
            "extra_buck_dependencies": {
                "build-dependencies": [
                    "//eden/scm/lib/util/python-sysconfig:python-sysconfig",
                    "fbsource//third-party/rust:cc",
                ],
                "dependencies": [
                    (None, "//common/rust/shed/fbinit:fbinit"),
                ],
            },
            "features": {
                "default": [],
                "eden": [
                    "sapling-clidispatch/eden",
                    "commands/eden",
                ],
                "fb": ["commands/fb"],
                "sl_oss": [
                    "sapling-clidispatch/sl_oss",
                    "commands/sl_oss",
                ],
                "with_chg": [
                    "dirs",
                    "sapling-identity",
                ],
            },
            "package": {
                "build": "build.rs",
                "name": "hgmain",
            },
            "profile": {"release": {
                "debug": True,
                "incremental": True,
                "lto": False,
            }},
        },
        "cargo_toml_dir": "exec/hgmain",
    },
    extra_features = select({
        "DEFAULT": [],
        "ovr_config//os:linux": [] if rust_oss.is_oss_build() else ["cas"],
    }),
    link_group_map = select({
        "DEFAULT": None,
        "ovr_config//build_mode/constraints:dev": EVICT_FBCODE_DEFAULT_DEV_GROUP_MAP,
    }),
    link_group_min_binary_node_count = select({
        "DEFAULT": None,
        "ovr_config//build_mode/constraints:dev": PYTHON_EXTREME_LINK_GROUP_THRESHOLD,
    }),
)

hg_binary(
    name = "hg_test",
    allow_oss_build = False,
    autocargo = {"ignore_rule": True},
)

rust_universal_binary(
    name = "hg_universal_binary",
    source = ":hg",
    visibility = ["PUBLIC"],
)

cpp_library(
    # @autodeps-skip
    name = "chg",
    srcs = [
        "contrib/chg/chg.c",
        "contrib/chg/hgclient.c",
        "contrib/chg/procutil.c",
        "contrib/chg/util.c",
    ],
    headers = [
        "contrib/chg/hgclient.h",
        "contrib/chg/procutil.h",
        "contrib/chg/util.h",
    ],
    compatible_with = [
        "ovr_config//os:linux",
        "ovr_config//os:macos",
    ],
)

buck_command_alias(
    name = "runhg",
    exe = ":hg",
)

buck_sh_binary(
    name = "scm_prompt",
    main = "contrib/scm-prompt.sh",
)

buck_sh_binary(
    name = "zsh_completion",
    main = "contrib/zsh_completion",
)

buck_genrule(
    name = "paths.d_mercurial",
    out = "mercurial",
    bypass_restricted_cmd = True,
    cmd = "echo '/opt/facebook/hg/bin' > $OUT",
    compatible_with = [
        "ovr_config//os:linux",
        "ovr_config//os:macos",
    ],
)

buck_genrule(
    name = "system.rc",
    out = "system.rc",
    bypass_restricted_cmd = True,
    cmd = 'echo "%include include_for_rpm.rc" > $OUT',
    compatible_with = [
        "ovr_config//os:linux",
        "ovr_config//os:macos",
    ],
)

cpp_binary(
    name = "whochanges",
    srcs = [
        "contrib/whochanges/whochanges.c",
    ],
    compatible_with = [
        "ovr_config//os:linux",
    ],
    external_deps = [
        ("glibc", None, "rt"),
    ],
)

python_binary(
    name = "update_staleness_detectors",
    srcs = ["fb/update_staleness_detectors.py"],
    main_module = "eden.scm.fb.update_staleness_detectors",
    deps = [
        "fbsource//third-party/pypi/click:click",
        "//libfb/py/asyncio:limit_concurrency",
        "//libfb/py/pyre:pyre",
    ],
)
